﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FluentScheduler;
using YiYiTong.Rocket.Helper;
using YiYiTong.Rocket.Business;
using YiYiTong.Rocket.Model;
using YiYiTong.Rocket.EFDBFirst;

namespace YiYiTong.Rocket.ProcessConsole
{
    /// <summary>
    /// 过期商品，库存卖完商品自动下架
    /// </summary>
    public class GoodsJob : BaseJob
    {
        static int excutecounts;
        public override void DoAction()
        {
            if (GoodsJob.excutecounts % 1000 == 0)
            {
                LogHelper.Debug($"过期商品，库存卖完商品自动下架处理：{GoodsJob.excutecounts}次");
            }
            GoodsJob.excutecounts++;

            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            var goodsList = dbcontext.ADZ_GoodsMaster.Where(t => t.is_del == 0 && t.is_withdraw == 0);
            if (goodsList != null)
            {
                foreach (var item in goodsList.ToList())
                {
                    var gooodsLabel = dbcontext.ADZ_GoodsLabel.FirstOrDefault(t => t.goods_id == item.goods_id);
                    if (gooodsLabel != null)
                    {
                        //过期商品自动下架
                        if (gooodsLabel.end_time != null && gooodsLabel.end_time != DateTime.MinValue)
                        {
                            if (gooodsLabel.end_time < DateTime.Now)
                                GoodsWithdrawDown(item, "到期商品自动下架");
                        }
                    }

                    //库存小于最小订单量
                    if (item.stock_amount < item.min_amount)
                    {
                        ////最后下单时间至现在已经超过24小时
                        //if (item.end_order_time.HasValue && item.end_order_time.Value.AddHours(24) < DateTime.Now)
                        //{
                        //不存在未支付的订单，直接下架
                        var order_ids = from t in dbcontext.ADZ_OrderDetails
                                        where t.goods_id == item.goods_id
                                        select t.order_id;
                        if (order_ids != null)//有订单，但是没有待支付的
                        {
                            var orderList = dbcontext.ADZ_OrderMaster.Where(t => t.is_del == 0 && t.order_status == (int)OrderStatus.WaitPay && order_ids.Contains(t.order_id));
                            if (orderList == null)
                                GoodsWithdrawDown(item, "库存小于最小订单量自动下架");
                        }
                        else //没有订单
                            GoodsWithdrawDown(item, "库存小于最小订单量自动下架");
                        //}
                    }
                    if (item.is_withdraw == 1)
                    {
                        LogHelper.Debug($"自动下架：{item.MPN}");
                    }
                }
                dbcontext.SaveChanges();
            }
        }

        private void GoodsWithdrawDown(ADZ_GoodsMaster goods,string reason)
        {
            goods.is_withdraw = 1;
            goods.draw_down_reason = reason;
            goods.with_down_time = DateTime.Now;
            goods.with_down_time = DateTime.Now;
        }
    }
}
